home *** CD-ROM | disk | FTP | other *** search
/ A Teacher's Guide to the Holocaust / A Teacher's Guide to the Holocaust.iso / data / people / scripts / elemtimrclass.js < prev    next >
Text File  |  1999-12-05  |  8KB  |  282 lines

  1. // Copyright 1998,1999 Macromedia, Inc. All rights reserved.
  2.  
  3. //Constructs a timer element
  4. function MM_timr(theParent, theName, theInitialValue,
  5.                  theNumberOfFrames, theTimeLimit,
  6.                  theReverseFrames) {
  7.   // properties
  8.   this.initialValue = theInitialValue;
  9.   this.value = '';
  10.   this.disabled = true;
  11.  
  12.   this.timeLeft = '';
  13.   this.timeLimit = theTimeLimit;
  14.   this.timeStart = '';
  15.  
  16.   this._parent = theParent;
  17.   this._name = theName;
  18.   this._self = theParent._self+".e['"+theName+"']";
  19.   this._obj = '';
  20.   this._fileExt = '';
  21.   this._filePre = '';
  22.   this._preloadFrame = 1;
  23.   this._preloadInterval = 1;
  24.   this._preloadObj = new Image();
  25.   this._tick = (theTimeLimit/theNumberOfFrames)/3;
  26.   this._numberOfFrames = theNumberOfFrames;
  27.   this._currentFrame = 0;
  28.   this._reverseFrames = theReverseFrames;
  29.   this._timeoutWaiting = false;
  30.   this.c = new Array();
  31.  
  32.   // member functions
  33.   this.init = MM_timrInit;
  34.   this.reset = MM_timrReset;
  35.   this.enable = MM_timrEnable;
  36.   this.disable = MM_timrDisable;
  37.   this.setDisabled = MM_timrSetDisabled;
  38.   this.update = MM_timrUpdate;
  39.   this.setValue = MM_timrSetValue;
  40.  
  41.   this.step = MM_timrStep;
  42.   this.getClosestFrame = MM_timrGetClosestFrame;
  43.   this.waitTick = MM_timrWaitTick;
  44.   this.preloadFrame = MM_timrPreloadFrame;
  45.   this.getFrameSrc = MM_timrGetFrameSrc;
  46.  
  47. }
  48.  
  49. // Initializes the element
  50. function MM_timrInit() {
  51.   var theSrc, extIndex, undIndex;
  52.   with (this) {
  53.     _obj = MM_intFindObject(_parent._self + _name + "Img");
  54.     if (_obj && _obj.src != null) {
  55.       theSrc = _obj.src;
  56.       extIndex = theSrc.lastIndexOf(".");
  57.       if (extIndex != -1) { // save the extension
  58.         _fileExt = theSrc.substring(extIndex, theSrc.length);
  59.         theSrc = theSrc.substring(0, extIndex); // remove extension
  60.       }
  61.       undIndex = theSrc.lastIndexOf("_");
  62.       if (undIndex != -1)
  63.         _filePre = theSrc.substring(0, undIndex+1);
  64.  
  65.       preloadFrame();
  66.     }
  67.     if (_tick > 100) _tick = 100;
  68.   }
  69. }
  70.  
  71. //Resets the element
  72. function MM_timrReset() {
  73.   var i;
  74.   with (this) {
  75.     value = initialValue;
  76.     timeLeft = Math.max(0, timeLimit - value);
  77.     for(i in c) if (i != 'length' && c[i].expectedValue >= value)
  78.       c[i].selected = c[i].judged = false;
  79.     if (_obj) {
  80.       _currentFrame = getClosestFrame(value);
  81.       _obj.src = getFrameSrc(_currentFrame);
  82.       preloadFrame();
  83.     }
  84.     timeStart = Math.floor((new Date()).getTime()/1000) - value;
  85.     _parent.disabled ? disable() : enable();
  86.   }
  87. }
  88.  
  89. //Enables the element
  90. function MM_timrEnable() {
  91.   var i;
  92.   with (this) {
  93.     if (disabled) {
  94.       disabled = false;
  95.       timeStart = Math.floor((new Date()).getTime()/1000) - value;
  96.     }
  97.     for (i in c) if (i != 'length') c[i].disabled = false;
  98.     update();
  99.     waitTick();
  100.   }
  101. }
  102.  
  103. //Disables the element
  104. function MM_timrDisable() {
  105.   with (this) {
  106.     if (!disabled) {
  107.       disabled = true;
  108.       value = Math.floor((new Date()).getTime()/1000) - timeStart;
  109.       timeLeft = timeLimit - value;
  110.   } }
  111. }
  112.  
  113. //Calls the approppriate disable or enable function
  114. function MM_timrSetDisabled(theDisabled) {
  115.   if (theDisabled) this.disable();
  116.   else this.enable();
  117. }
  118.  
  119. //Called by onClick event to update this elements value
  120. function MM_timrUpdate() {
  121.   var i,judgeInt=false;
  122.  
  123.   with (this) {
  124.     if (disabled) return;
  125.  
  126.     // walk the list of choices, setting selected
  127.     for (i in c) if (i != 'length') {
  128.       if (!c[i].judged && c[i].validValue()) {
  129.         judgeInt = true;
  130.         c[i].judged = true;
  131.     } }
  132.  
  133.     // call the parent's update
  134.     _parent.update(!judgeInt);
  135.   }
  136. }
  137.  
  138. function MM_timrSetValue(theValue) {
  139.   with (this) {
  140.     value = Math.max(0, theValue);
  141.     timeStart = Math.floor((new Date()).getTime()/1000) - value;
  142.     timeLeft = Math.max(0, timeLimit - value);
  143.     for(i in c) if (i != 'length' && c[i].expectedValue >= value)
  144.       c[i].selected = c[i].judged = false;
  145.     if (_obj) {
  146.       _currentFrame = getClosestFrame(value);
  147.       _obj.src = getFrameSrc(_currentFrame);
  148.       preloadFrame();
  149.     }
  150.     _parent.update(true); // update int, no judge
  151.   }
  152. }
  153.  
  154. //Calls the setTimeout() function for this element.
  155. function MM_timrWaitTick() {
  156.   with (this) {
  157.     if (!_timeoutWaiting) {
  158.       _timeoutWaiting = true;
  159.       setTimeout(_self + ".step()", _tick);
  160.   } }
  161. }
  162.  
  163. //Returns the "best guess" of the frame to show for the current time.
  164. // theTime - the time to find the frame for in seconds
  165. function MM_timrGetClosestFrame(theTime) {
  166.   var frame;
  167.   with (this) {
  168.     frame = Math.floor((theTime * (_numberOfFrames - 1)) / timeLimit);
  169.     if (theTime >= timeLimit)
  170.       frame = _numberOfFrames - 1;
  171.     else if (frame > (_numberOfFrames - 2))
  172.       frame = _numberOfFrames - 2;
  173.   }
  174.   return frame;
  175. }
  176.  
  177. //Returns the URL for the given frame image.
  178. // theFrame - the frame to get the source file name for
  179. function MM_timrGetFrameSrc(theFrame) {
  180.   var iTest, numberStr;
  181.   with (this) {
  182.     numberStr = (_reverseFrames) ? (_numberOfFrames - theFrame) : (theFrame + 1);
  183.     // add pad zeros
  184.     for (iTest=10; (this._numberOfFrames >= iTest); iTest*=10)
  185.       if (numberStr < iTest) numberStr = "0" + numberStr;
  186.   }
  187.   return this._filePre + numberStr + this._fileExt;
  188. }
  189.  
  190. //Preload the "best guess" next frame that we can use.
  191. function MM_timrPreloadFrame() {
  192.   with (this) {
  193.     _preloadFrame = _currentFrame + _preloadInterval;
  194.     if (_preloadFrame >= _numberOfFrames)
  195.       _preloadFrame = _numberOfFrames - 1;
  196.     _preloadObj.src = getFrameSrc(_preloadFrame);
  197.   }
  198. }
  199.  
  200. //Catches the timeout event and updates the timer display
  201. // and element values.  Args:
  202. function MM_timrStep() {
  203.   var doPreload, closestFrame;
  204.   with (this) {
  205.     _timeoutWaiting = false;
  206.     if (!disabled) {
  207.       value = Math.floor((new Date()).getTime()/1000) - timeStart;
  208.       timeLeft = timeLimit - value;
  209.       if (_obj) {
  210.         doPreload=false;
  211.         closestFrame=getClosestFrame(value);
  212.         if (_preloadObj.complete) {
  213.           if (closestFrame >= _preloadFrame) {
  214.             _obj.src = _preloadObj.src;
  215.             doPreload = true;
  216.           } 
  217.         } else if (closestFrame > _preloadFrame) {
  218.           _preloadInterval++;
  219.           doPreload = true;
  220.         } 
  221.         if (doPreload) {
  222.           _currentFrame = closestFrame;
  223.           preloadFrame();
  224.       } }
  225.       
  226.       if (this.onTick != null) onTick(_parent._self+_name, value);
  227.  
  228.       waitTick();
  229.       update();
  230.   } }
  231. }
  232.  
  233.  
  234. /////////////////////////////////////////
  235. //Create a range choice object
  236. function MM_timrTrig(theParent, theElement, theName,
  237.                      theExpectedValue, theIsCorrect, theScore) {
  238.   // properties
  239.   this.expectedValue = theExpectedValue;
  240.   this.isCorrect = theIsCorrect;
  241.   this.score = theScore;
  242.   this.selected = false;
  243.   this.disabled = false;
  244.  
  245.   this.judged = false;
  246.  
  247.   this._elem = eval(theParent._self+".e['"+theElement+"']");
  248.   this._isChoice = true;
  249.  
  250.   // method
  251.   this.validValue = MM_timrTrigValidValue;
  252.   this.setDisabled = MM_timrTrigSetDisabled;
  253.   this.setSelected = MM_timrTrigSetSelected;
  254. }
  255.  
  256. //Returns true if value is above this threshold.
  257. function MM_timrTrigValidValue() {
  258.   with (this) {
  259.     selected = false;
  260.     if (!disabled && _elem.value != null)
  261.       selected = (expectedValue <= _elem.value);
  262.   }
  263.   return this.selected;
  264. }
  265.  
  266. function MM_timrTrigSetDisabled(theDisabled) {
  267.   with (this) {
  268.     disabled = theDisabled;
  269.     validValue();
  270.     _elem._parent.update(true); // update int, no judge
  271.   }
  272. }
  273.  
  274. function MM_timrTrigSetSelected(theSelected) {
  275.   with (this) {
  276.     if (theSelected) {
  277.       _elem.setValue(expectedValue);
  278.     } else {
  279.       this.selected = false;
  280.       _elem._parent.update(true); // update int, no judge
  281.   } }
  282. }